﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MvcControlToolkit
{
    /// <summary>
    /// MutuallyExclusiveCheckBox is an AJAX extender that can be attached to any 
    /// CheckBox control. By adding a number of checkboxes to the same "Key", only 
    /// one checkbox with the specified key can be checked at a time. 
    /// 
    /// This extender is useful when a number of choices are available but only one 
    /// can be chosen, similar to a radio button. The use of checkboxes however allows 
    /// you to choose to uncheck a value which is not possible normally with radio 
    /// buttons. This also provides a more consistent and expected interface than 
    /// using javascript to allow the de-selection of a RadioButton item
    /// </summary>
    public static class MutuallyExclusiveCheckBoxExtensions
    {
        /// <summary>
        /// Creates a MutuallyExclusiveCheckBox extender
        /// </summary>
        /// <param name="elementId">The ID of the CheckBox to modify</param>
        /// <param name="groupKey">The unique key to use to associate checkboxes</param>
        public static string MutuallyExclusiveCheckBox(this AjaxHelper helper, string elementId, string groupKey)
        {
            return helper.MutuallyExclusiveCheckBox(
                    elementId,
                    new { Key = groupKey }
                );
        }

        /// <summary>
        /// Creates a MutuallyExclusiveCheckBox extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the CheckBox to modify</param>
        /// <param name="properties">An object containing the properties to be set</param>        
        public static string MutuallyExclusiveCheckBox(this AjaxHelper helper, string elementId, object properties)
        {
            var sb = new StringBuilder();

            // Add Microsoft Ajax library   
            sb.Append(helper.MicrosoftAjaxInclude());

            // Add toolkit scripts   
            sb.Append(helper.ToolkitInclude(
                    "AjaxControlToolkit.ExtenderBase.BaseScripts.js",
                    "AjaxControlToolkit.Common.Common.js",
                    "AjaxControlToolkit.MutuallyExclusiveCheckBox.MutuallyExclusiveCheckBoxBehavior.js"
                ));

            // Perform $create               
            sb.AppendLine(helper.Create("AjaxControlToolkit.MutuallyExclusiveCheckBoxBehavior", properties, elementId));

            return sb.ToString();
        }
    }
}
